<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class CreateCarmanagersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //所属区域 市
        Schema::create('cities', function (Blueprint $table) {
            $table->Integer('id')->unsigned()->autoIncrement();
            $table->string('name')->comment('车辆所属区域')->unique();
            $table->timestamps();
        });

        //所属区域 区
            Schema::create('carareas', function (Blueprint $table) {
            $table->Integer('id')->unsigned()->autoIncrement();
            $table->string('name')->comment('车辆所属区域')->unique();
            $table->Integer('city_id')->unsigned()->comment('所属区域')->nullable();
            $table->foreign('city_id')->references('id')->on('cities')->onDelete('set null');
            $table->timestamps();
        });
        //车辆类型
        Schema::create('carkinds', function (Blueprint $table) {
            $table->Integer('id')->unsigned()->autoIncrement();
            $table->string('name')->comment('车辆类型的名称')->unique();
            $table->timestamps();
        });


        //部件类型
        Schema::create('car_components', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->Integer('kind_id')->unsigned()->comment('所属类型')->nullable(true);
            $table->string('detail')->unique()->comment('部件细类');
            $table->foreign('kind_id')->references('id')->on('carkinds')->onDelete('set null');
            $table->string('desc')->comment('备注')->nullable(true);
            $table->timestamps();
        });

        //车辆信息
        Schema::create('cars', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('number')->comment('车辆识别码')->unique()->nullable();
            $table->string('brand')->comment('车辆品牌')->nullable();
            $table->string('plates_number')->unique()->comment('车牌号');
            $table->dateTime('card_time')->comment('上牌时间')->nullable();
            $table->dateTime('buy_time')->comment('购买时间')->default(DB::raw('CURRENT_TIMESTAMP'));
            $table->dateTime('register_time')->comment('登记时间')->default(DB::raw('CURRENT_TIMESTAMP'));
            $table->string('format')->comment('车辆规格')->nullable();
            $table->string('framenumber')->comment('车架号')->nullable();
            $table->string('identify_number')->comment('车辆识别码')->nullable();
            $table->Integer('department_id')->unsigned()->comment('所属部门')->nullable();
            $table->Integer('use_department_id')->unsigned()->comment('所属部门')->nullable();
            $table->unsignedBigInteger('kinds_id')->unsigned()->comment('所属类型')->nullable();
            $table->Integer('areas_id')->unsigned()->comment('所属区域')->nullable();
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('set null');
            $table->foreign('use_department_id')->references('id')->on('departments')->onDelete('set null');
            $table->foreign('kinds_id')->references('id')->on('car_components')->onDelete('set null');
            $table->foreign('areas_id')->references('id')->on('carareas')->onDelete('set null');
            $table->string('desc')->comment('备注信息')->nullable();
            $table->Integer('car_user')->comment('当值司机')->unsigned()->nullable();
            $table->foreign('car_user')->references('id')->on('employees')->onDelete('set null');
            $table->Integer('car_manager')->nullable()->comment('负责人')->unsigned();
            $table->foreign('car_manager')->references('id')->on('employees')->onDelete('set null');
            $table->unsignedTinyInteger('status')->nullable()->comment('车辆状态 1. 调拨 2.借用 3.使用中 4. 停运');
            $table->timestamps();
        });


        //年检记录的附件
        Schema::create('car_invoice_files', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('f_id')->comment('图片所属的发票的信息')->nullable();
            $table->foreign('f_id')->on('cars')->references('id')->onDelete('set null');
            $table->unsignedBigInteger('file_id')->comment('图片所属的文件信息')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id')->onDelete('set null');
            $table->unique(['f_id','file_id'],'bf_unique');
        });


        //年检记录的附件
        Schema::create('car_quality_files', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('f_id')->comment('图片所属的发票的信息')->nullable();
            $table->foreign('f_id')->on('cars')->references('id')->onDelete('set null');
            $table->unsignedBigInteger('file_id')->comment('图片所属的文件信息')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id')->onDelete('set null');
            $table->unique(['f_id','file_id'],'bf1_unique');
        });


        //年检记录的附件
        Schema::create('car_vehicle_license_files', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('f_id')->comment('图片所属的发票的信息')->nullable();
            $table->foreign('f_id')->on('cars')->references('id')->onDelete('set null');
            $table->unsignedBigInteger('file_id')->comment('图片所属的文件信息')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id')->onDelete('set null');
            $table->unique(['f_id','file_id'],'bf2_unique');
        });


        //年检记录的附件
        Schema::create('car_registration_files', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('f_id')->comment('图片所属的发票的信息')->nullable();
            $table->foreign('f_id')->on('cars')->references('id')->onDelete('set null');
            $table->unsignedBigInteger('file_id')->comment('图片所属的文件信息')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id')->onDelete('set null');
            $table->unique(['f_id','file_id'],'bf3_unique');
        });


        //年检记录的附件
        Schema::create('car_maintenance_files', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('f_id')->comment('图片所属的发票的信息')->nullable();
            $table->foreign('f_id')->on('cars')->references('id')->onDelete('set null');
            $table->unsignedBigInteger('file_id')->comment('图片所属的文件信息')->nullable();
            $table->foreign('file_id')->on('file_upload')->references('id')->onDelete('set null');
            $table->unique(['f_id','file_id'],'bf4_unique');
        });

        //车辆调拨申请记录
        Schema::create('cars_rent',function (Blueprint $table){
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('car_id')->unsigned()->comment('申请车辆')->nullable();
            $table->foreign('car_id')->on('cars')->references('id')->onDelete('set null');
            $table->unsignedInteger('car_user')->comment('申请人')->unsigned()->nullable();
            $table->foreign('car_user')->references('id')->on('employees')->onDelete('set null');
            $table->Integer('department_id')->unsigned()->comment('申请部门')->nullable();
            $table->foreign('department_id')->references('id')->on('departments')->onDelete('set null');
            $table->dateTime('start_time')->comment('开始时间');
            $table->dateTime('end_time')->comment('结束时间');
            $table->unsignedTinyInteger('apply_status')->comment('申请状态 1. 审批中 2.已通过 3.未通过 4.通过但是被撤销 5.已执行成功 6. 未执行成功 联系程序员')->default(1);
            $table->string('desc')->comment('备注信息')->nullable();
            $table->string('status')->nullable()->comment('车辆状态 1. 调拨 2.借用 3.使用中 4. 停运');
            $table->unsignedTinyInteger('kind')->comment('分类 1. 调拨 2.借用')->default(1);
        });

        //车辆信息 设备管理 设备种类
        Schema::create('equipment_kinds', function (Blueprint $table) {
            $table->Integer('id')->unsigned()->autoIncrement();
            $table->string('name')->unique()->comment('设备种类');
            $table->timestamps();
        });


        //设备信息
        Schema::create('equipments', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name')->comment('设备名称')->nullable();
            $table->string('number')->comment('设备编码')->nullable()->unique();
            $table->string('brand')->comment('设备品牌')->nullable();
            $table->Integer('kind_id')->unsigned()->comment('设备种类的id号')->nullable();
            $table->foreign('kind_id')->references('id')->on('equipment_kinds')->onDelete('set null');
            $table->bigInteger('car_id')->unsigned()->comment('设备绑定的车辆');
            $table->foreign('car_id')->references('id')->on('cars')->onDelete('cascade');
            $table->boolean('custom')->default(false)->comment('加工定制');
            $table->timestamps();

        });

        //队列任务
        Schema::create('queue_tasks',function (Blueprint $table){
            $table->bigIncrements('id');
            $table->string('class_name')->comment('执行的class');
            $table->string('function_name')->comment('执行的函数');
            $table->string('args')->comment('参数');
            $table->timestamp('exec_time')->comment('任务执行时间')->nullable();
            $table->string('value')->comment('期待执行时间戳')->nullable();
            $table->boolean('is_action')->comment('任务是否取消')->default(true);
            $table->boolean('is_consume')->comment('任务是否已经执行')->default(false);
            $table->boolean('is_accepted')->comment('现在是否被队列接受执行')->default(false);
            $table->timestamps();
        });

        //队列任务
        Schema::create('vertex',function (Blueprint $table){
            $table->bigIncrements('nodeID');
            $table->decimal('lon',20,10)->comment('经度')->nullable();
            $table->decimal('lat',20,10)->comment('经度')->nullable();
            $table->string('adjacencyNodes',32)->comment('未知')->nullable();
            $table->string('roads',128)->comment('道路命')->nullable();
            $table->string('distance',64)->comment('未知')->nullable();
        });

        Schema::create('car_tasks', function (Blueprint $table) {
            //字段定义
            $table->increments('id');
            $table->string('task_name')->comment('任务名称');
            $table->integer('task_type')->unsigned()->comment('任务类型  0-环卫保洁 1-园林养护 2-路面洒水 3-垃圾清理');
            $table->string('task_content')->comment('任务详细');
            $table->integer('user_id')->unsigned()->comment('人员')->nullable();
            $table->foreign('user_id')->references('id')->on('employees')->onDelete('set null');
            $table->bigInteger('car_id')->unsigned()->comment('任务车辆id')->nullable(true);
            $table->foreign('car_id')->references('id')->on('cars')->onDelete('set null');
            $table->timestamp('task_begin_time')->comment('任务开始时间')->nullable(true);
            $table->timestamp('task_end_time')->comment('任务结束时间')->nullable(true);
            $table->integer('task_level')->unsigned()->comment('紧急程度  1-一级 2-二级 3-三级')->default(3);
            $table->integer('task_status')->unsigned()->comment('任务状态 0=待完成 1=处理中 2=已完成')->default(0);
            $table->softDeletes();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {

        Schema::dropIfExists('car_tasks');
        Schema::dropIfExists('vertex');
        Schema::dropIfExists('queue_tasks');
        Schema::dropIfExists('equipments');
        Schema::dropIfExists('equipment_kinds');
        Schema::dropIfExists('car_check_files');
        Schema::dropIfExists('car_check');
        Schema::dropIfExists('cars_rent');
        Schema::dropIfExists('car_maintenance_files');
        Schema::dropIfExists('car_registration_files');
        Schema::dropIfExists('car_vehicle_license_files');
        Schema::dropIfExists('car_quality_files');
        Schema::dropIfExists('car_invoice_files');
        Schema::dropIfExists('cars');
        Schema::dropIfExists('car_components');
        Schema::dropIfExists('carkinds');
        Schema::dropIfExists('carareas');
        Schema::dropIfExists('cities');

    }

}
